package com.libertyville.domain.orgmng;

import com.libertyville.domain.orgmng.validator.CancelOrderValidator;
import com.libertyville.domain.orgmng.validator.LeaderValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class OrgHandler {
    private LeaderValidator leaderValidator;
    private CancelOrderValidator cancelOrderValidator;

    @Autowired
    public OrgHandler(LeaderValidator leaderValidator, CancelOrderValidator cancelOrderValidator) {
        this.leaderValidator = leaderValidator;
        this.cancelOrderValidator = cancelOrderValidator;
    }

    public void updateBasic(Org org, String newName, Integer newLeaderId, String modifiedBy) {
        org.setName(newName);
        updateLeader(org, newLeaderId);
        updateAudit(org, modifiedBy);
    }

    private void updateLeader(Org org, Integer newLeaderId) {
        leaderValidator.validate(newLeaderId);
        org.setLeaderId(newLeaderId);
    }

    private void updateAudit(Org org, String modifiedBy) {
        org.setLastUpdatedAt(LocalDateTime.now());
        org.setLastUpdatedBy(modifiedBy);
    }

    public void cancel(Org org, String modifiedBy) {
        cancelOrderValidator.validate(org);
        org.cancel();
        updateAudit(org, modifiedBy);
    }
}
